﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	internal static class GenParse_ReportType_Fire
	{
		#region private constants

		internal const byte MsgBlockOnlyValue = 0x10;

		private const byte GeneralByte = 0x00;
		private const byte ManualByte = 0x01;
		private const byte AutomaticByte = 0x02;
		private const byte ExtinguishingSystemByte = 0x03;

		#endregion private constants

		#region internal methods

		internal static byte GenerateByte(ReportType_Fire reportTypeFire)
		{
			byte resultByte = 0x0;

			// Fire has 0x1 in Bits 6,5,4
			resultByte += MsgBlockOnlyValue;

			// add the report type sub type
			if (reportTypeFire.ReportSubType == eReportSubType_Fire.General)
			{
				resultByte |= GeneralByte;
				return resultByte;
			}

			if (reportTypeFire.ReportSubType == eReportSubType_Fire.Manual)
			{
				resultByte |= ManualByte;
				return resultByte;
			}

			if (reportTypeFire.ReportSubType == eReportSubType_Fire.Automatic)
			{
				resultByte |= AutomaticByte;
				return resultByte;
			}

			if (reportTypeFire.ReportSubType == eReportSubType_Fire.ExtinguishingSystem)
			{
				resultByte |= ExtinguishingSystemByte;
				return resultByte;
			}

			// TODO handle error (unknown eReportSubType_Fire)
			return resultByte;
		}

		internal static bool ParseByte(byte byteValue, out eReportSubType_Fire reportSubTypeFire)
		{
			// set arbitary default value
			reportSubTypeFire = eReportSubType_Fire.General;

			// get only bits 6,5,4
			byte messageBlock = (byte)(byteValue & (byte)0x70);
			if (messageBlock == MsgBlockOnlyValue)
			{
				// get only bits 3,2,1,0
				byte subType = (byte)(byteValue & (byte)0x0f);
				if (subType == GeneralByte)
				{
					reportSubTypeFire = eReportSubType_Fire.General;
					return true;
				}

				if (subType == ManualByte)
				{
					reportSubTypeFire = eReportSubType_Fire.Manual;
					return true;
				}

				if (subType == AutomaticByte)
				{
					reportSubTypeFire = eReportSubType_Fire.Automatic;
					return true;
				}

				if (subType == ExtinguishingSystemByte)
				{
					reportSubTypeFire = eReportSubType_Fire.ExtinguishingSystem;
					return true;
				}

				// bits 3,2,1,0 are wrong
				return false;
			}

			// bits 6,5,4 are wrong
			return false;
		}

		#endregion internal methods
	}
}
